<?php

namespace app\admin\model;

use app\admin\common\Common;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\Model;

class AppModel extends Model
{
    protected $pk = 'appid';

    public static function updateapp($data){
        $update = [
            'appname' => $data['appname'],
            'appicon' => $data['appicon'],
            'introduction' => $data['introduction'],
            'author' => $data['author'],
            'group' => $data['group'],
            'title' => $data['title'],
            'content' => $data['content'],
            'version' => $data['version'],
            'updatecontent' => $data['updatecontent'],
            'download' => $data['download'],
            'zcmoney' => $data['zcmoney'],
            'zcexp' => $data['zcexp'],
            'zcvip' => $data['zcvip'],
            'signmoney' => $data['signmoney'],
            'signexp' => $data['signexp'],
            'signvip' => $data['signvip'],
            'postmoney' => $data['postmoney'],
            'postexp' => $data['postexp'],
            'commentmoney' => $data['commentmoney'],
            'commentexp' => $data['commentexp'],
            'is_email' => $data['is_email'],
            'hierarchy' => $data['hierarchy'],
            'app_site_status' => $data['app_site_status'],
        ];
        try {
            $app = Db::name('app')->where('appid', $data['appid'])->update($update);
        } catch (PDOException $e) {
             return Common::result(400,$e->getMessage());
        } catch (Exception $e) {
             return Common::result(400,$e->getMessage());
        }
        if ($app>0){
            return Common::result(200,'修改成功');
        }else{
            return Common::result(400,'你未做任何修改！');
        }
    }

    public static function addapp($data){
        $adddata = [
            'appname' => $data['appname'],
            'creattime' => date("Y-m-d H:i:s",time()),
        ];
        $app = Db::name('app')->data($adddata)->insert();
        if ($app>0){
            return Common::result(200,'添加成功');
        }else{
            return Common::result(400,'添加失败');
        }
    }

    public static function delapp($data){
        try {
            $app = Db::name('app')->delete($data['appid']);
        } catch (PDOException $e) {
             return Common::result(400,$e->getMessage());
        } catch (Exception $e) {
             return Common::result(400,$e->getMessage());
        }
        if ($app>0){
            return Common::result(200,"删除成功");
        }else{
            return Common::result(400,'删除失败');
        }
    }

    public static function searchapp($data){
        try {
            $sql = Db::name('app')
                ->where('appname', $data['appname'])
                ->select();
        } catch (DataNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (ModelNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (DbException $e) {
             return Common::result(400,$e->getMessage());
        }
        if (sizeof($sql) == 0){
            return Common::result(400,"暂无查询到数据");
        }else{
            return Common::result(200,"查询成功",$sql);
        }
    }
}